package descentFly.app
{
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	
	import descentFly.app.dialogs.df_baseLoginWindow;
	import descentFly.app.dialogs.df_chatWindow;
	import descentFly.app.dialogs.df_editLoginWindow;
	import descentFly.app.dialogs.df_enterLoginWindow;
	import descentFly.app.dialogs.df_garageWindow;
	import descentFly.app.dialogs.df_magazineWindow;
	import descentFly.app.dialogs.df_optionsWindow;
	import descentFly.app.dialogs.df_startMenu;
	import descentFly.app.dialogs.levelsWindow.df_battleParams;
	import descentFly.app.dialogs.levelsWindow.df_battlesWindow;
	import descentFly.app.dialogs.levelsWindow.df_levelItem;
	import descentFly.app.dialogs.levelsWindow.df_onLineBattles;
	import descentFly.app.managePanel.df_playerInfo;
	import descentFly.app.managePanel.df_transButton;
	import descentFly.app.radar.df_radarMonitor;
	import descentFly.app.radar.df_radarPanel;
	import descentFly.away3D4.df_awaySpace;
	import descentFly.away3D4.net.df_netSupport;
	import descentFly.view.df_view;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	
	import jiglib.debug.Stats;
	
	public class df_userInterface extends df_miniInterface {
		private var _levelWindow		: df_battlesWindow;
		private var _onlineBatWindow	: df_battlesWindow;
		private var _optionsWindow		: df_optionsWindow;
		private var _magazineWindow		: df_magazineWindow;
		private var _garageWindow		: df_garageWindow;
		private var _loginWindow		: df_baseLoginWindow;
		private var _chatWindow			: df_chatWindow;
		private var _chatButton			: df_transButton;
		private var _startMenu			: df_startMenu;
		private var _onLineBattles		: df_onLineBattles;
		
		public function df_userInterface(parent: uif_component, rect: Rectangle) {
			super(parent, rect);
			/*
			_chatButton = new df_transButton(this, new Rectangle(225, rect.height - 110, 20, 100));
			_chatButton.addEvent(MouseEvent.CLICK, doChatClick);
			_chatButton.setVisibility(false);
			*/
			
			_levelWindow 	= new df_battlesWindow(this, 600, 402, locale.LEVELS, Space.levels, df_levelItem, df_events.LOAD_LEVEL, 1);
			_optionsWindow 	= new df_optionsWindow(this);
			_magazineWindow = new df_magazineWindow(this);
			_garageWindow 	= new df_garageWindow(this);
			//_chatWindow		= new df_chatWindow(this);
			_startMenu		= new df_startMenu(this);
			
			//if (Space && dfApp.params.debug) debugLayer();
		}
		
		protected function debugLayer(): void {
			var a_view: df_view = (app as away_jiglib).view;
			var stats: Stats = new Stats(a_view.view, (Space as df_awaySpace).physic);
			stats.y = stage.stageHeight - 126;
			stats.x = stage.stageWidth - 200;
			sceneComponents.addChild(stats);
		}
		
		private function doChatClick(e: Event): void {
			_chatWindow.show();
		}
		
		protected function showLoginWindow(lwClass: Class, description: String=''): df_baseLoginWindow {
			function doClose(e: Event): void {
				_loginWindow = null;
			}
			if (!_loginWindow) {
				_loginWindow  = new lwClass(this, description);
				_loginWindow.show();
				_loginWindow.addEvent(df_window.CLOSE, doClose);
			}
			return _loginWindow;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(df_events.SHOW_STARTMENU, function(e: Event): void {
				_startMenu.show();
			});
			
			addEvent(df_events.SHOW_LEVELS, function(e: Event): void {
				if (dfApp.player.login) _levelWindow.show();
				else showLoginWindow(df_enterLoginWindow);
			});
			
			addEvent(df_events.SHOW_OPTIONS, function(e: Event): void {
				_optionsWindow.show();
			});
			
			addEvent(df_events.SHOW_MAGAZINE, function(e: Event): void {
				_magazineWindow.show();
			});
			
			addEvent(df_events.SHOW_GARAGE, function(e: Event): void {
				_garageWindow.show();
			});
			
			addEvent(df_events.SETLOGIN, function(e: Event): void {
				showLoginWindow(df_editLoginWindow);
			});

			addEvent(df_events.SHOW_LOGIN, function(e: Event): void {
				showLoginWindow(df_enterLoginWindow);
			});
			
			addEvent(df_events.SHOW_ONLINELEVELS, function(e: Event): void {
				showOnLineBattles();
			});
			
			addEvent(df_events.SHOW_BATTLEPARAMS, function(e: ObjectEvent): void {
				showOnLineBattleParams(e.object);
			});
			
/*			
			addEvent(df_netSupport.ENTER_SESSION,  function (e: Event): void {
				_chatButton.slideShow();
			}, stage);
			
			addEvent(df_netSupport.EXIT_SESSION, function (e: Event): void {
				_chatButton.slideHide();
			}, stage);
			
			addEvent(df_events.ENTERUSER, function(e: Event): void {
				_chatButton.blink();
			}, stage);
			
			addEvent(df_events.RECEIVECHATMESSAGE, function(e: Event): void {
				_chatButton.blink();
			}, stage);
*/			
		}
		
		private function showOnLineBattleParams(a_battle: Object): void {
			(new df_battleParams(This, a_battle)).show();
		}
		
		private function showOnLineBattles(): void {
			if (_onLineBattles) _onLineBattles.close();
			dfApp.player.getOnlineBattles(function(battles: Object): void {
				_onLineBattles = new df_onLineBattles(This, 600, 402, locale.ONLINELEVELS, battles, 
					df_levelItem, df_events.LOAD_ONLINEBATTLE, 2);
				_onLineBattles.show();
			});
		}
	}
}